/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


(function () {
    var overLook = function (config) {

        this._config = config;
        this._type = this.getConfig("type"); //公文类别
        this._tableName = this.getConfig("tableName");
        this._typeReal = this.getConfig("typeReal");
        this._id = this.getConfig("id");
        this._taskName = this.getConfig("taskName");
        this._taskId = this.getConfig("taskId");
        this._relationId = this.getConfig("relationId") || "";
        this._screenindex = this.getConfig("screenindex") || 3;
        this.width = window.innerWidth || 320;
        this.height = window.innerHeight || 480;
    };
    if (typeof (Clickmed) === "undefined") {
        Clickmed = {};
    }
    Clickmed.overLook = overLook;
    overLook.prototype.init = function () {

        var me = this;
        var self = {
            taskId: me._taskId
        };
        var url = serverURL + "cmcp/phone/getCurrentUserTask";
        console.log(self);
        PostCP(url, self, function (dat) {
            console.log(dat);
            if (dat)
                me.forTask = dat["userTask"] || "";
            else {
                me.forTask = "";
            }
            me.lookAll();
        }, function () {
            me.forTask = "";
            me.lookAll();
        });
    };
    overLook.prototype.lookAll = function () {
        var me = this;
        baseurl = serverURL;
        var id = this._config.id || null;
        var taskId = this._config.taskId || null;
        var tableName = this._tableName || null;
        var typeReal = this._typeReal || null;
        var url = baseurl + "cmcp/phone/getDetailInfo";
        var self = {
            id: id,
            relationId: me._relationId,
            typeReal: typeReal,
            usertask: me.forTask,
            tableName: tableName
        };
        console.log(self);
        window.plugins.getIntent.show(["OA", "正在获取数据"])
        PostCP(url, self, function (dat) {
            window.plugins.getIntent.hide();
            if (!dat) {
                Alert("获取数据失败..");
                return;
            }
            me.draw(dat);
        }, function () {
            window.plugins.getIntent.hide();
            Alert("获取数据失败");
        })

    };
    overLook.prototype.draw = function (dat) {
        console.log(dat);
        var me = this;
        var render = this._config.render || d3.select("body");
        var d1 = this._config.body || d3.select("body");
        var width = this.width;
        var scrol = d1.append("div").classed("scrol", true).style({
            width: this.width + "px",
            height: this.height + "px",
            position: "absolute",
            left: "0px",
            top: "0px"
        }).attr("id", "scrol");
        var warp = scrol.append("div").classed("dt_warp", true).style({
            position: "relative"
        });
        var datong_header = warp.append("div").classed("datong_header", true);

        var go_back = datong_header.append("div").classed("go_back", true);
        go_back.html("<i><img src =../../common/page/images/go_back_ico.png width =16 height=auto></i>返回");
        go_back.on(touch_s, function () {
            $(this).css("background-color", "#005ebf");
        });
        go_back.on(touch_e, function () {
            $(this).css("background-color", "");
            if (typeof (GLJ) != "undefined" && GLJ) {
                window.closeView();
                return;
            }
            d1.selectAll("#scrol").remove();
            setTimeout(function () {

                Android.back = TempFunction.back;
            }, Transiacion);

        });
        // 左上角返回按钮动作 
        function back1() {

            d1.selectAll("#scrol").remove();
            setTimeout(function () {

            }, Transiacion);
            Android.back = TempFunction.back;
        }

        // Android返回键动作
        Android.back = TempFunction.back1 = back1;
        datong_header.append("h1").html(me._taskName || "查看来文");


        Redraw(dat);
        function Redraw(dat) {
            var detailInfo = dat["detailInfo"];
            var approvalInfo = dat["approvalInfo"];
            var attachmentInfo = dat["attachmentInfo"];
            var attachmentInfos = dat["attachmentInfos"] || null;
            var relations = dat["relations"] || null;
            var secOptionInfo = dat["secOptionInfo"] || null;
            var drafterInfos = dat["drafterInfos"] || null;
            var scrol_ = warp.append("div").classed("scrol_", true).style({
                width: "100%",
                height: height - 44 + "px",
                postion: "relative",
                overflow: "hidden"
            }).attr("id", "scrol_");
            var datong_con = scrol_.append("div").classed("datong_con datong_con_re", true);
            var datong_con = datong_con.append("div").classed("phone_cotent", true);
            var ul = datong_con.append("ul");
            var li = ul.append("li").classed("b_btm", true).style({
                width: "100%"
            });
            var table = li.append("table").attr("cellpadding", "0").attr('cellspacing', "0").style({
                width: "100%"
            });
            ;
            var sxFlag = false;
            for (var name in detailInfo) {
                if (name.indexOf("属性缘由") > -1) {
                    sxFlag = true;
                    continue;
                }
                if (name.indexOf("上卡标示") > -1) {
                    if (detailInfo[name] == "2") {
                        me._upFlag = true;
                    }

                    continue;
                }
                if (!detailInfo[name])
                    continue;
                //       console.log(name);
                var tr = table.append("tr");
                if (name.indexOf("raw") > -1) {
                    var str = "";
                    for (var raw in detailInfo[name]) {

                        var raw1 = raw.split("").join("&nbsp");

                        tr.append("td").html("<div >" + raw1 + "</div>").style({
                            width: "20%"
                        });
                        tr.append("td").style({
                            width: "22%"
                        }).append("div").html(detailInfo[name][raw]).classed("extend", true);
                    }
                    //   var dd = dl.append("dd").html(str);
                    continue;
                }


                var raw1 = name.split("").join("&nbsp");


                tr.append("td").html("<div>" + raw1 + "</div>").style({
                    width: "20%"
                });
               tr.append("td").attr("colspan", 3).append("div").html(detailInfo[name]).classed("extend", true);
                //  var dd = dl.append("dd").html(str);
            }
            if (sxFlag) {
                var tr = table.append("tr");
                tr.append("td").html("<div>" + "属&nbsp性&nbsp缘&nbsp由</div>").style({
                    width: "20%"
                });
                tr.append("td").attr("colspan", 3).append("div").html(detailInfo["属性缘由"]).classed("extend", true);
            }
            //       var report_title = datong_con.append("div").classed("report_title", true).html("领导批示:");
            for (var name in approvalInfo) {
                //    console.log(approvalInfo[name])
                if (!approvalInfo[name].length || approvalInfo[name] == {})
                    continue;
                var report_title = datong_con.append("div").classed("report_title", true).html(name);
                for (var nj in approvalInfo[name]) {
                    if (!approvalInfo[name][nj])
                        continue;
                    var ul = datong_con.append("ul");
                    var li = ul.append("li").classed("b_btm", true).style({
                        padding: "5px 0px"
                    });
                    var dl = li.append("dl");
                    dl.append("dd").html(approvalInfo[name][nj].replace(/\n/g, "<br>"));
                }
            }
            // 加载所有列表-edit by lichunzhao 20191110
            if(attachmentInfo){
            	for(var item in attachmentInfo){
            		var files = attachmentInfo[item];
            		if (files && files.length>0) {
                        var fileList = datong_con.append("div").classed("report_title", true).html(item);
                        for (var i = 0; i < files.length; i++) {
                            if (!files[i]["name"]){
                            	continue;
                            }
                            var newDiv = datong_con.append("div").classed("newDiv", true).attr("url", files[i]["url"]).attr("fileName", files[i]["name"]);
                            var len = files[i]["url"].split(".").length;
                            var extend = files[i]["url"].split(".")[len - 1];
                            var img = newDiv.append("img").attr("src", "../../common/page/images/" + extend + ".png").attr({
                                width: "25",
                                height: "22"
                            }).style("margin-left", "15px");
                            // 更改正文名字
                            var fileName = "";
                            if(item == "正文"){
                            	fileName = item;
                            }else{
                            	fileName = files[i]["name"];
                            }
                            var gragh = newDiv.append("p").html(fileName).attr("url", files[i]["url"]).attr("fileName", fileName);
                            // 打开正文附件
                            newDiv.on("click", function () {
                                if (Flag){return;}
                                Flag = true;
                                setTimeout(function () {Flag = false;}, 500);
                                window.openF(serverURL + $(this).attr("url").substr(1), $(this).attr("fileName"));
                            });
                        }
                    }
            	}
            } 

            // 是否隐藏顶部
            var detailInfo = dat["detailInfo"];
            var approvalInfo = dat["approvalInfo"];
            var attachmentInfo = dat["attachmentInfo"];
            if (!(objectJudge(detailInfo) || objectJudge(approvalInfo) || objectJudge(attachmentInfo))) {
                $(datong_con.node()).hide();
            }


            // 转办正文
            if (drafterInfos && drafterInfos.length) {
                for (var i = 0; i < drafterInfos.length; i++) {
                    var datong_con = d3.select(".datong_con_re").append("div").classed("phone_cotent", true);
                    me.addFile(datong_con, drafterInfos[i], true);
                }

            }
            // 下级来文附件，关联件
            if (attachmentInfos && attachmentInfos.length) {
                for (var index in attachmentInfos) {
                    var fileList = datong_con.append("div").classed("report_title", true).html((attachmentInfos[index]["单位"] + "") || "").style({
                        "font-size": "17px"
                    });
                    //  var fileList = datong_con.append("div").classed("report_title", true).html("正文：");
                    for (var i = 0, j = attachmentInfos[index]["正文"].length; i < j; i++) {
                        var newDiv = datong_con.append("div").classed("newDiv", true)
                            .attr("url", attachmentInfos[index]["正文"][i]["url"]).attr("fileName", attachmentInfos[index]["正文"][i]["name"]);
                        var len = attachmentInfos[index]["正文"][i]["url"].split(".").length;
                        var extend = attachmentInfos[index]["正文"][i]["url"].split(".")[len - 1];
                        var img = newDiv.append("img").attr("src", "../../common/page/images/" + extend + ".png").attr({
                            width: "25",
                            height: "22"
                        }).style("margin-left", "15px");
                        var file = newDiv.append("p").html("正文")
                            .attr("url", attachmentInfos[index]["正文"][i]["url"]).attr("fileName", attachmentInfos[index]["正文"][i]["name"]);
                        newDiv.on("click", function () {
                            var url = $(this).attr("url");
                            if (Flag)
                                return;
                            Flag = true;
                            setTimeout(function () {
                                Flag = false;
                            }, 500);
                            url = serverURL + url.substr(1);
                            var fileName = $(this).attr("fileName");
                            window.openF(url, fileName);
                        });
                    }
                    if (attachmentInfos[index]["附件"] && attachmentInfos[index]["附件"].length) {
                        var fileList = datong_con.append("div").classed("report_title1", true).html("附件：").style({
                            "padding-left": "20px",
                            "background": "#f7f7f7",
                            "font-size": "13px"
                        });
                    }

                    for (var i = 0, j = attachmentInfos[index]["附件"].length; i < j; i++) {
                        var newDiv = datong_con.append("div").classed("newDiv", true)
                            .attr("url", attachmentInfos[index]["附件"][i]["url"]).attr("fileName", attachmentInfos[index]["附件"][i]["name"]);
                        var len = attachmentInfos[index]["附件"][i]["url"].split(".").length;
                        var extend = attachmentInfos[index]["附件"][i]["url"].split(".")[len - 1];
                        var img = newDiv.append("img").attr("src", "../../common/page/images/" + extend + ".png").attr({
                            width: "25",
                            height: "22"
                        }).style("margin-left", "15px");
                        var file = newDiv.append("p").html(attachmentInfos[index]["附件"][i]["name"])
                            .attr("url", attachmentInfos[index]["附件"][i]["url"]).attr("fileName", attachmentInfos[index]["附件"][i]["name"]);
                        newDiv.on("click", function () {
                            var url = $(this).attr("url");
                            if (Flag)
                                return;
                            Flag = true;
                            setTimeout(function () {
                                Flag = false;
                            }, 500);
                            url = serverURL + url.substr(1);
                            var fileName = $(this).attr("fileName");
                            window.openF(url, fileName);
                        });
                    }

                }
            }
            if (relations && relations.length) {
                var k = relations.length;

                if ((me._userTask == "usertask9") && me._businessType == "0012") {
                    if (relations.length <= 1) {
                        k = 0;
                    }
                    else {
                        var datong_con = d3.select(".datong_con_re").append("div").classed("phone_cotent", true);
                        var fileList = datong_con.append("div").classed("report_title", true).html("关联件");
                        k--;
                    }
                }
                else {
                    var datong_con = d3.select(".datong_con_re").append("div").classed("phone_cotent", true);
                    var fileList = datong_con.append("div").classed("report_title", true).html("关联件");

                }
                for (var i = 0, j = relations.length; i < k; i++) {
                    var file = datong_con.append("p").style("color", "blue").style("line-height", "30px").style({
                        "margin-left": "15px",
                        "margin-right": "15px"
                    }).html(relations[i]["unit"] + ":  " + relations[i]["title"]).attr("id", relations[i]["id"]);
                    file.on("click", function () {
                        if (Flag)
                            return;
                        Flag = true;

                        var id = $(this).attr("id");
                        var type = me._type || null;
                        var typeReal = me._typeReal || null;
                        var taskName = me._taskName || null;
                        var taskId = me._taskId || "";
                        var relationId = me._id;
                        window.localStorage.taskName = taskName;
                        var url = baseurl + "cmcp/phone/getDetailInfo";
                        var self = {
                            id: id,
                            typeReal: typeReal,
                            type: type
                        };
                        var href = location.href;
                        var len = href.lastIndexOf("/");
                        var url = href.substr(0, len - 4) + "guanlj/guanlj.html?id=" + id + "&type=" + type + "&typeReal=" + typeReal + "&taskId=" + taskId + "&relationId=" + relationId + "&screenindex=3";
                        window.openViewG(encodeURI(url));
                        setTimeout(function () {
                            Flag = false;
                        }, 500);

                    });
                }

            }
            // 为页面下方增加一点空隙
            d3.select(".datong_con_re").append("p").style({
                height: "15px",
                "text-align": "center",
                background: "",
                opacity: 0,
                width: "100%"
            }).html("").attr("id", "append");
            /* 流程跟踪
             */
            var report_title = d3.select(".datong_con_re").append("div").classed("report_title", true).html("流程跟踪");
            report_title.style({
                background: "#f2f2f0",
                color: "#2345e6",
                "border-radius": "10px",
                "margin": "0% 2% 2% 2%"
            })
            report_title.on("click", function () {
                if (Flag)
                    return;
                Flag = true;
                me.openNew();

                setTimeout(function () {
                    Flag = false;
                }, 500);
            });
            Yscroll = new iScroll("scrol_", {hScrollbar: false, vScrollbar: false});
            window.plugins.getIntent.hide();
        }

    };
    overLook.prototype.addFile = function (datong_con, drafterInfos, flag) {
        var datong_con = datong_con
        var detailInfo = drafterInfos

        var attachmentInfo = drafterInfos;
        if (!flag) {
            datong_con.append("div").classed("report_title", true).html("回复单位")
        }
        var ul = datong_con.append("ul");
        var li = ul.append("li").classed("b_btm", true).style({
            width: "100%"
        });
        var table = li.append("table").attr("cellpadding", "0").attr('cellspacing', "0").style({
            width: "100%"
        });
        ;
        var sxFlag = false;
        for (var name in detailInfo) {
            if (name == "正文" || name == "附件")
                continue;
            if (name.indexOf("属性缘由") > -1) {
                sxFlag = true;
                continue;
            }
            if (!detailInfo[name])
                continue;
            //       console.log(name);
            var tr = table.append("tr");
            if (name.indexOf("raw") > -1) {
                var str = "";
                for (var raw in detailInfo[name]) {

                    var raw1 = raw.split("").join("&nbsp");

                    tr.append("td").html("<div>" + raw1 + "</div>").style({
                        width: "20%"
                    });
                    tr.append("td").attr({
                        width: "22%"
                    }).append("div").html(detailInfo[name][raw]).classed("extend", true);
                    ;
                }
                //   var dd = dl.append("dd").html(str);
                continue;
            }


            var raw1 = name.split("").join("&nbsp");


            tr.append("td").html("<div>" + raw1 + "</div>").style({
                width: "20%"
            });
            tr.append("td").attr("colspan", 3).append("div").html(detailInfo[name]).classed("extend", true);
            //  var dd = dl.append("dd").html(str);
        }
        if (sxFlag) {
            var tr = table.append("tr");
            tr.append("td").html("<div>" + "属&nbsp性&nbsp缘&nbsp由</div>").style({
                width: "20%"
            });
            tr.append("td").attr("colspan", 3).append("div").html(detailInfo["属性缘由"]).classed("extend", true);
        }

        // 正文列表

        if (true) {
            if (attachmentInfo && attachmentInfo["正文"]) {
                var fileList = datong_con.append("div").classed("report_title", true).html("正文");
                var newDiv = datong_con.append("div").classed("newDiv", true)
                    .attr("url", attachmentInfo["正文"]["url"]).attr("fileName", attachmentInfo["正文"]["name"]);
                var len = attachmentInfo["正文"]["url"].split(".").length;
                var extend = attachmentInfo["正文"]["url"].split(".")[len - 1];
                var img = newDiv.append("img").attr("src", "../../common/page/images/" + extend + ".png").attr({
                    width: "25",
                    height: "22"

                }).style("margin-left", "15px");
                // 更改正文名字
                var gragh = newDiv.append("p").html("正文")
                    .attr("url", attachmentInfo["正文"]["url"]).attr("fileName", attachmentInfo["正文"]["name"]);
                // 打开正文附件
                newDiv.on("click", function () {
                    if (Flag)
                        return;
                    Flag = true;
                    setTimeout(function () {
                        Flag = false;
                    }, 500);
                    var url = $(this).attr("url");
                    var fileName = $(this).attr("fileName");
                    url = serverURL + url.substr(1);
                    //   Alert(url);
                    window.openF(url, fileName);
                });

            }
        }

        // 附件列表
        if (attachmentInfo && attachmentInfo["附件"] && attachmentInfo["附件"].length) {
            var fileList = datong_con.append("div").classed("report_title", true).html("附件");
            for (var i = 0; i < attachmentInfo["附件"].length; i++) {
                var newDiv = datong_con.append("div").classed("newDiv", true)
                    .attr("url", attachmentInfo["附件"][i]["url"]).attr("fileName", attachmentInfo["附件"][i]["name"]);
                var len = attachmentInfo["附件"][i]["url"].split(".").length;
                var extend = attachmentInfo["附件"][i]["url"].split(".")[len - 1];
                var img = newDiv.append("img").attr("src", "../../common/page/images/" + extend + ".png").attr({
                    width: "25",
                    height: "22"

                }).style("margin-left", "15px");
                var file = newDiv.append("p").html(attachmentInfo["附件"][i]["name"])
                    .attr("url", attachmentInfo["附件"][i]["url"]).attr("fileName", attachmentInfo["附件"][i]["name"]);
                // 打开附件
                newDiv.on("click", function () {
                    var url = $(this).attr("url");
                    if (Flag)
                        return;
                    Flag = true;
                    setTimeout(function () {
                        Flag = false;
                    }, 500);
                    url = serverURL + url.substr(1);
                    var fileName = $(this).attr("fileName");

                    window.openF(url, fileName);

                });
            }
        }
    };
    overLook.prototype.openNew = function () {
        var me = this;
        var href = location.href;
        var len = href.lastIndexOf("views");
        var url = href.substr(0, len + 6) + "lcgz/lcgz.html?id=" + me._id + "&typeReal=" + me._typeReal + "&relationId=" + me._relationId + "&screenindex=" + me._screenindex;
        window.openViewC(url);
    };
    overLook.prototype.getConfig = function (key) {
        if (this._config == null) {
            return null;
        } else {
            if (typeof (this._config[key]) != "undefined") {
                return this._config[key];
            } else {
                return null;
            }
        }
    };
})();